package com.paomi.client.utils;

import android.app.Activity;

import com.paomi.client.net.Constants;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.Map;

/**
 * Created by pc on 2018/12/5
 * Describe:
 * com.paomi.client.utils
 */
public class PayUtil {
    /**
     * 微信支付
     * @param activity 当前Activity
     * @param pre 上一页Activity
     * @param data
     */
    public static void payForWx(Activity activity, Activity pre, Map<String, String> data) {
        IWXAPI api = WXAPIFactory.createWXAPI(activity, Constants.APP_ID);
        PayReq req = new PayReq();
        req.appId = data.get("appid");
        req.partnerId = data.get("partnerid");
        req.prepayId = data.get("prepayid");
        req.nonceStr = data.get("noncestr");
        req.timeStamp = data.get("timestamp");
        req.packageValue = data.get("package");
        req.sign = data.get("sign");
        req.extData = "app data"; // optional
        api.registerApp(Constants.APP_ID);
        // 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
        api.sendReq(req);
        if (pre != null) {
            pre.finish();
        }
//        activity.finish();
    }

}
